<?php

class Sukien extends CI_Controller {
    
    function __construct() {
        parent::__construct();
		$this->load->library('form_validation');		
		$this->load->helper(array('form','url','codegen_helper'));
		$this->load->model('codegen_model','',TRUE);
                $this->load->helper('ckeditor');

        

        $this->data['ckeditor_1'] = array(
            //ID of the textarea that will be replaced
            'id' => 'noidung_tomtat',
            'path' => 'js/ckeditor',
            //Optionnal values
            'config' => array(
                'width' => "550px", //Setting a custom width
                'height' => '100px', //Setting a custom height
                'toolbar' => array(//Setting a custom toolbar
                    array('Bold', 'Italic'),
                    array('Underline', 'Strike', 'FontSize'),
                    array('Smiley'),
                    '/'
                )
            ),
            //Replacing styles from the "Styles tool"
            'styles' => array(
                //Creating a new style named "style 1"
                'style 3' => array(
                    'name' => 'Green Title',
                    'element' => 'h3',
                    'styles' => array(
                        'color' => 'Green',
                        'font-weight' => 'bold'
                    )
                )
            )
        );
        $this->data['ckeditor_2'] = array(
            //ID of the textarea that will be replaced
            'id' => 'noidungchinh',
            'path' => 'js/ckeditor',
            //Optionnal values
            'config' => array(
                'width' => "550px", //Setting a custom width
                'height' => '100px', //Setting a custom height
                'toolbar' => array(//Setting a custom toolbar
                    array('Bold', 'Italic'),
                    array('Underline', 'Strike', 'FontSize'),
                    array('Smiley'),
                    '/'
                )
            ),
            //Replacing styles from the "Styles tool"
            'styles' => array(
                //Creating a new style named "style 1"
                'style 3' => array(
                    'name' => 'Green Title',
                    'element' => 'h3',
                    'styles' => array(
                        'color' => 'Green',
                        'font-weight' => 'bold'
                    )
                )
            )
        );
	}	
	
	function index(){
		$this->manage();
	}

	function manage(){
        $this->load->library('table');
        $this->load->library('pagination');
        
        //paging
        $config['base_url'] = base_url().'index.php/sukien/manage/';
        $config['total_rows'] = $this->codegen_model->count('sukien');
        $config['per_page'] = 10;	
        $this->pagination->initialize($config); 	
        // make sure to put the primarykey first when selecting , 
		//eg. 'userID,name as Name , lastname as Last_Name' , Name and Last_Name will be use as table header.
		// Last_Name will be converted into Last Name using humanize() function, under inflector helper of the CI core.
		$this->data['results'] = $this->codegen_model->get('sukien','masukien,tensukien,noidung_tomtat,noidungchinh,hinh_sukien,kich_hoat','',$config['per_page'],$this->uri->segment(3));
       
	   $this->load->view('admin/sukien/sukien_list', $this->data); 
       //$this->template->load('content', 'sukien_list', $this->data); // if have template library , http://maestric.com/doc/php/codeigniter_template
		
    }
	
    function add(){        
        $this->load->library('form_validation');   
        $this->data['custom_error'] = '';
        $this->load->library('upload');    
		
		
        if ($this->form_validation->run('sukien') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {           
            if (isset($_FILES)) {
                $name = $_FILES['hinh_sukien']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/sukienimg/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinh_sukien')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            };
            if(!empty($name)){
                $data = array(
                        'tensukien' => set_value('tensukien'),
                                            'noidung_tomtat' => set_value('noidung_tomtat'),
                                            'noidungchinh' => set_value('noidungchinh'),
                                            'hinh_sukien' =>$name,
                                            'kich_hoat' => set_value('kich_hoat')
                );
            }else {
               $data = array(
                        'tensukien' => set_value('tensukien'),
                                            'noidung_tomtat' => set_value('noidung_tomtat'),
                                            'noidungchinh' => set_value('noidungchinh'),
                                            'kich_hoat' => set_value('kich_hoat')
                );
            }
           
			if ($this->codegen_model->add('sukien',$data) == TRUE)
			{
				//$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
				// or redirect
				redirect(base_url().'index.php/sukien/manage/');
			}
			else
			{
				$this->data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';

			}
		}		   
		$this->load->view('admin/sukien/sukien_add', $this->data);   
        //$this->template->load('content', 'sukien_add', $this->data);
    }	
    
    function edit(){        
        $this->load->library('form_validation'); 
        $this->load->library('upload');    
        
		$this->data['custom_error'] = '';
        if ($this->form_validation->run('sukien') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {   
            if (isset($_FILES)) {
                $name = $_FILES['hinh_sukien']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/sukienimg/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('hinh_sukien')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            };
            if($name !=''){
               $data = array(
                         'tensukien' => $this->input->post('tensukien'),
					'noidung_tomtat' => $this->input->post('noidung_tomtat'),
					'noidungchinh' => $this->input->post('noidungchinh'),
					'hinh_sukien' => $name,
					'kich_hoat' => $this->input->post('kich_hoat')
                );
            }else{
                $data = array(
                         'tensukien' => $this->input->post('tensukien'),
					'noidung_tomtat' => $this->input->post('noidung_tomtat'),
					'noidungchinh' => $this->input->post('noidungchinh'),
					'kich_hoat' => $this->input->post('kich_hoat')
                );
            }
           
			if ($this->codegen_model->edit('sukien',$data,'masukien',$this->input->post('masukien')) == TRUE)
			{
				redirect(base_url().'index.php/sukien/manage/');
			}
			else
			{
				$this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>';

			}
		}

		$this->data['result'] = $this->codegen_model->get('sukien','masukien,tensukien,noidung_tomtat,noidungchinh,hinh_sukien,kich_hoat','masukien = '.$this->uri->segment(3),NULL,NULL,true);
		
		$this->load->view('admin/sukien/sukien_edit', $this->data);		
        //$this->template->load('content', 'sukien_edit', $this->data);
    }
	
    function delete(){
            $ID =  $this->uri->segment(3);
            $this->codegen_model->delete('sukien','masukien',$ID);             
            redirect(base_url().'index.php/sukien/manage/');
    }
}

/* End of file sukien.php */
/* Location: ./system/application/controllers/sukien.php */